package com.leyou.system.controller;

import com.leyou.common.domain.PageResult;
import com.leyou.system.dto.SysUserDto;
import com.leyou.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class UserController {

	@Autowired
	private UserService userService;

	@GetMapping("/member/page")
	public ResponseEntity<PageResult<SysUserDto>> userPageQuery(@RequestParam(value = "page", defaultValue = "1") Integer page,
																@RequestParam(value = "rows", defaultValue = "50") Integer rows,
																@RequestParam(value = "key",required = false) String key,
																@RequestParam(value = "status", defaultValue = "true") Boolean status,
																@RequestParam(value = "sortBy",required = false) String sortBy,
																@RequestParam(value = "desc",required = false,defaultValue = "true") Boolean desc) {
		PageResult<SysUserDto> userList = userService.userPageQuery(page, rows, key, status, sortBy, desc);
		return ResponseEntity.ok(userList);
	}

	@PostMapping("/member")
	public ResponseEntity<Void> addUser(@RequestBody SysUserDto sysUserDto) {
		userService.addUser(sysUserDto);
		return ResponseEntity.status(HttpStatus.OK).build();
	}

	@GetMapping("/member/{id}")
	public ResponseEntity<SysUserDto> findUserById(@PathVariable("id") String id) {
		SysUserDto user = userService.findUserById(id);
		return ResponseEntity.ok(user);
	}


    @PutMapping("/member")
    public ResponseEntity<Void> editUser(@RequestBody SysUserDto sysUserDto) {
        userService.updateUser(sysUserDto);
		return ResponseEntity.status(HttpStatus.OK).build();
    }

	@DeleteMapping("/member/{id}")
	public ResponseEntity<Void> deleteUserById(@PathVariable Integer id) {
		userService.deleteUserById(id);
		return ResponseEntity.status(HttpStatus.CREATED).build();
	}

	@DeleteMapping
	public ResponseEntity<Void> deleteMoreUser(@RequestParam("ids") List<Integer> ids){
		//System.out.println(ids);
		userService.deleteUserByIds(ids);
		return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
	}
}
